﻿using MySql.Data.MySqlClient;
using ProjectCommonLibs.enums;
using ProjectCommonLibs.models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ProjectCommonLibs.utils
{
    public class DataBaseUtils
    {
        private readonly MySqlConnection connection = new MySqlConnection();
        private MySqlCommand command;
        private CloudUtils cloud;

        public DataBaseUtils(string connectionString,string remoteAddress)
        {
            connection.ConnectionString = connectionString;
            if (!String.IsNullOrEmpty(remoteAddress))
            {
                cloud = new CloudUtils(remoteAddress);
            }
        }

        public bool openConnection()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(connection.ConnectionString);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Message);
                return false;
            }

        }
        public bool closeConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(connection.ConnectionString);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Message);
                return false;
            }
        }
        public bool insertMysql(MysqlTables table, string createTime, List<long> data,string[] otherInfos = null)
        {
            try
            {
                var tableName = getTableName(table);
                var tableData = getDataString(data);
                var others = "";
                if (otherInfos != null)
                {
                    others = string.Join("','", otherInfos);
                    others = "'" + others + "',";
                }
                var insertSql = $"insert into {tableName} values(null,'{createTime}',{others}{tableData})";
                command = new MySqlCommand(insertSql, connection);
                var result = command.ExecuteNonQuery();
                if (result == 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return false;
            }

        }

        public static string getTableName(MysqlTables tables)
        {
            switch (tables)
            {
                case MysqlTables.A_POWER:
                    return "converter_a_power";
                case MysqlTables.B_POWER:
                    return "converter_b_power";
                case MysqlTables.B_ENERGY:
                    return "converter_b_energy";
                case MysqlTables.A_ENERGY:
                    return "converter_a_energy";
                case MysqlTables.A_QUALITY:
                    return "converter_a_quality";
                case MysqlTables.B_QUALITY:
                    return "converter_b_quality";
                case MysqlTables.A_DC_CURRENT:
                    return "converter_a_current";
                case MysqlTables.B_DC_CURRENT:
                    return "converter_b_current";
                case MysqlTables.DC_VOLTA:
                    return "converter_volta";
                case MysqlTables.LOCAL_PHOTOVOLTAIC:
                    return "local_photovoltaic";
                case MysqlTables.LOCAL_WEATHER:
                    return "local_weather";
                case MysqlTables.DTU_AMMETER:
                    return "dtu_ammeter";
                case MysqlTables.DTU_AMMETER2:
                    return "ammeter_power";
                default:
                    throw new Exception("类型错误");
            }
        }
        public static string getDataString(List<long> v)
        {
            return String.Join<long>(",", v);
        }

        public void withSql(string tableName,string sql,string cloudMessage)
        {
            try
            {
                command = new MySqlCommand(sql, connection);
                var result = command.ExecuteNonQuery();
                if (cloud != null)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(cloud.insertData), new HttpRequestModel()
                    {
                        tableName = tableName,
                        time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        data = cloudMessage
                    });
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        public void insertData(IDataModel model, MysqlTables table, string[] otherInfos = null)
        {
            int count = 0;
            bool fulfillCondition = false;
            List<long> data;
            string time;
            switch (table)
            {
                case MysqlTables.A_POWER:
                    count = 29;
                    data = (model as DongTaiDataModel).converterAPower;
                    time = (model as DongTaiDataModel).createTime;
                    fulfillCondition = !DataCheckUtils.isAllDataZero(data);
                    break;
                case MysqlTables.B_POWER:
                    count = 29;
                    data = (model as DongTaiDataModel).converterBPower;
                    time = (model as DongTaiDataModel).createTime;
                    fulfillCondition = !DataCheckUtils.isAllDataZero(data);
                    break;
                case MysqlTables.B_ENERGY:
                    count = 20;
                    data = (model as DongTaiDataModel).converterBEnergy;
                    time = (model as DongTaiDataModel).createTime;
                    fulfillCondition = !DataCheckUtils.isAllDataZero(data);
                    break;
                case MysqlTables.A_ENERGY:
                    count = 20;
                    data = (model as DongTaiDataModel).converterAEnergy;
                    time = (model as DongTaiDataModel).createTime;
                    fulfillCondition = !DataCheckUtils.isAllDataZero(data);
                    break;
                case MysqlTables.A_QUALITY:
                    count = 186;
                    data = (model as DongTaiDataModel).converterAQuality;
                    time = (model as DongTaiDataModel).createTime;
                    fulfillCondition = !DataCheckUtils.isAllDataZero(data);
                    break;
                case MysqlTables.B_QUALITY:
                    count = 186;
                    data = (model as DongTaiDataModel).converterBQuality;
                    time = (model as DongTaiDataModel).createTime;
                    fulfillCondition = !DataCheckUtils.isAllDataZero(data);
                    break;
                case MysqlTables.A_DC_CURRENT:
                    count = 8;
                    data = (model as DongTaiDataModel).converterACurrent;
                    time = (model as DongTaiDataModel).createTime;
                    fulfillCondition = !DataCheckUtils.isAnyDataZero(data);
                    break;
                case MysqlTables.B_DC_CURRENT:
                    count = 8;
                    data = (model as DongTaiDataModel).converterBCurrent;
                    time = (model as DongTaiDataModel).createTime;
                    fulfillCondition = !DataCheckUtils.isAnyDataZero(data);
                    break;
                case MysqlTables.DC_VOLTA:
                    count = 2;
                    data = (model as DongTaiDataModel).converterVolta;
                    time = (model as DongTaiDataModel).createTime;
                    fulfillCondition = !DataCheckUtils.isAnyDataZero(data);
                    break;
                case MysqlTables.LOCAL_PHOTOVOLTAIC:
                    count = 34;
                    data = (model as RoofPhotovoltaicModel).converterData;
                    time = (model as RoofPhotovoltaicModel).time;
                    fulfillCondition = !DataCheckUtils.isAllDataZero(data);
                    break;
                case MysqlTables.LOCAL_WEATHER:
                    count = 6;
                    data = (model as RoofWeatherModel).weatherData;
                    time = (model as RoofWeatherModel).time;
                    fulfillCondition = !DataCheckUtils.isAllDataZero(data);
                    break;
                case MysqlTables.DTU_AMMETER:
                    count = 27;
                    data = (model as DTUAmmeterModel).data;
                    time = (model as DTUAmmeterModel).time;
                    fulfillCondition = !DataCheckUtils.isAllDataZero(data);
                    break;
                default:
                    return;
            }
            if (data.Count == count && fulfillCondition)
            {
                insertMysql(table, time, data,otherInfos);
                if (cloud != null)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(cloud.insertData), new HttpRequestModel()
                    {
                        tableName = getTableName(table),
                        time = time,
                        data = getDataString(data)
                    });
                }
            }
        }
    }
}
